from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
# Create your views here.
class Staffviwe(APIView):
    def get(self,request):
        '''获取员工信息'''
        data=Staff.objects.all()
        list=[]
        for i in data:
            list.append({
                'id':i.id,
                'name':i.name,
                'duty':i.duty_id,
                'duty_name':i.duty.name,
                'department':i.department_id,
                'department_name':i.department.name,
                'phone':i.phone,
                'email':i.email,
                'census_register':i.census_register
            })
        return Response(list)

#添加
    def post(self,request):
        uname=request.data.get('name')
        department=request.data.get('department')
        duty=request.data.get('duty')
        if not all([uname,department,duty]):
            return Response({'code':400,'msg':'不完整'})
        department=Department.objects.filter(name=department)
        if not department:
            return Response({'code':400,'msg':'不存在'})
        duty=Duty.objects.filter(name=duty)
        if not duty:
            return Response({'code':400,'msg':'没有'})
        try:
            Staff.objects.create(department=department[0],duty=duty[0],name=uname[0])
        except Exception as e:
            return Response({'code':500,'msg':'添加错误'})
        return Response({'code':200,'msg':'添加成功'})

class StaffInview(APIView):
    #获取
    def get(self,request,name):
        staff=Staff.objects.filter(name=name)
        if not staff:
            return Response({'code':400,'msg':'员工不存在'})
        data={
            'id':staff[0].id,
            'name':staff[0].name,
            'department':staff[0].department.name,
            'duty':staff[0].duty.name
        }
        return Response({'code':200,'msg':'获取成功','data':data})

#修改
    def put(self,request,name):
        staff = Staff.objects.filter(name=name)
        if not staff:
            return Response({'code': 400, 'msg': '员工不存在'})
        phone=request.data.get('mobile')
        duty_name=request.data.get('duty')
        department_name=request.data.get('department')
        if not all([phone,duty_name,department_name]):
            return Response({'code':400,'msg':'不完整'})
        d_name=Duty.objects.filter(name=duty_name)
        if not d_name:
            return Response({'code':400,'msg':'没有职位'})
        p_name=Department.objects.filter(name=department_name)
        if not p_name:
            return Response({'code':400,'msg':'没有部门'})
        try:
            staff[0].phone=phone
            staff[0].department=p_name[0]
            staff[0].duty=d_name[0]
            staff[0].save()
            return Response({'code':200,'msg':'修改成功'})
        except Exception as e:
            return Response({'code':400,'msg':'修改失败'})

# class Staffuiview(APIView):
#     def get(self,request):
#         name=request.query_params.get('name')
#         data=Staff.objects.filter(name=name)
#         list=[]
#         for i in data:
#             list.append({
#
#             })
#         return Response(list)